package cn.stimd.spring.aop.support.annotation;

import cn.stimd.spring.aop.support.StaticMethodMatcher;
import org.springframework.util.ClassUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * Created by Stimd
 * 项目地址：https://gitee.com/stimd/spring-wheel
 * 公众号：Java编程探微
 */
public class AnnotationMethodMatcher extends StaticMethodMatcher {
    private final Class<? extends Annotation> annotationType;

    public AnnotationMethodMatcher(Class<? extends Annotation> annotationType) {
        this.annotationType = annotationType;
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        //当前类的方法上是否存在指定注解
        if (method.isAnnotationPresent(this.annotationType)) {
            return true;
        }

        //接口的方法上是否存在指定注解
        Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
        return (specificMethod != method && specificMethod.isAnnotationPresent(this.annotationType));
    }
}
